/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jpaintball;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Observable;
import javax.swing.Timer;

/**
 *
 * @author ondra
 */
public class Game extends Observable implements ActionListener {
    
    private Timer t;
    private double frametime;

    // static objects - bunkers
    ArrayList<Bunker> bunkers;
    // players
    ArrayList<Player> players;
    // paintballs
    ArrayList<Paintball> paintballs;

    Game() {

        // set game frametime (50 FPS)
        frametime = 0.015d;

        // initialize lists of objects
        bunkers = new ArrayList<Bunker>();
        players = new ArrayList<Player>();
        paintballs = new ArrayList<Paintball>();

        //place bunkers
        FieldLayout.placeBunkers(bunkers);

        // setup timer
        t = new Timer((int)(frametime*1000), this);
        t.start();

    }

    public int addPlayer() {
        Player p = new Player(10,10,frametime);
        players.add(p);
        return players.indexOf(p);
    }

    public void setPlayerInput(InputController _input, int index) {
        players.get(index).setInput(_input);
    }

    public void shoot(int index) {
        paintballs.add(players.get(index).createPaintball());
    }

    private void gameLoop() {

        // FIRST: Tell players and paintballs to move
        Iterator<Player> itPlayers = players.iterator();
        while (itPlayers.hasNext()) {
            itPlayers.next().move();
        }
        Iterator<Paintball> itPaintballs = paintballs.iterator();
        while (itPaintballs.hasNext()) {
            itPaintballs.next().move();
        }

        // SECOND: Check for collisions
        // brute force: check every player and paintball against every bunker
        //it = movables.iterator();
        //while(it.hasNext()) {
        //    MovableGameObject mgo = it.next();
        //    Iterator<Bunker> it2 = bunkers.iterator();
        //    while(it2.hasNext()) {
        //        Bunker b = it2.next();
        //        if(mgo.collidesWith(b)){
        //           mgo.setCollisionWith(b);
        //       }
        //    }
        //
        //}

        // THIRD: Delete destroyed paintballs
        itPaintballs = paintballs.iterator();
        while (itPaintballs.hasNext()) {
            if(itPaintballs.next().isDestroyed()) itPaintballs.remove();
        }

        // FOURTH: Put all object into one arraylist and pass them to observers
        ArrayList<GameObject> objects = new ArrayList();
        itPlayers = players.iterator();
        while (itPlayers.hasNext()) {
            objects.add(itPlayers.next());
        }
        itPaintballs = paintballs.iterator();
        while (itPaintballs.hasNext()) {
            objects.add(itPaintballs.next());
        }
        Iterator<Bunker> itBunkers = bunkers.iterator();
        while (itBunkers.hasNext()) {
            objects.add(itBunkers.next());
        }

        // Notify observers
        setChanged();
        notifyObservers(objects);

    }
    

    public void actionPerformed(ActionEvent e) {
        gameLoop();
    }

    
}
